<?php
declare (strict_types=1);
/**
 * @Description:
 * @Author: Chern
 * @Date: 2023/7/5
 * @Time: 10:01
 */

namespace app\service;

/*
 * rsa非对称数据加密
 */
class Rsa
{

    private static $PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAq4HKQWXDLH/sx8v3WiPR
QMx/z9SZb6fYM+tgYwXdslEOzThnrU4WE2CaUpRi5LOGCJsfJvo6LsVe+nLBD2Zb
0nru9a0HzrpSttJowU9BUo/oZldzshZTStH+SCBZhC15Oqy2U2JIpsEHB0d3O30Y
wtNBNt117JN5Xaat1PYDYQQ+ogvVcTEfbTc06YX4NJc0X9NhUED5Wnh4MTjYy/6T
GiOFDdUd8bersb90XGRF2KZLJGBMEcVQmlh+V071AKfu3m5Zsskiv4pcApavJ8LB
NKkndtymgJjBwWd3iSFSzhtKVoq7cuCL/uC50m2db1vKFOdBmtyauDRLSHQKbhCy
kwIDAQAB
-----END PUBLIC KEY-----
";


    private static $PRIVATE_KEY = "-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCrgcpBZcMsf+zH
y/daI9FAzH/P1Jlvp9gz62BjBd2yUQ7NOGetThYTYJpSlGLks4YImx8m+jouxV76
csEPZlvSeu71rQfOulK20mjBT0FSj+hmV3OyFlNK0f5IIFmELXk6rLZTYkimwQcH
R3c7fRjC00E23XXsk3ldpq3U9gNhBD6iC9VxMR9tNzTphfg0lzRf02FQQPlaeHgx
ONjL/pMaI4UN1R3xt6uxv3RcZEXYpkskYEwRxVCaWH5XTvUAp+7eblmyySK/ilwC
lq8nwsE0qSd23KaAmMHBZ3eJIVLOG0pWirty4Iv+4LnSbZ1vW8oU50Ga3Jq4NEtI
dApuELKTAgMBAAECggEAD6DgIV14j5GIrhw0Cg7Bfroj23hwEvCkjvaf7eYW8azZ
QHInYG+BK3reFTBYYqbJS/d2u0KKEsEBiq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tsRxP3V09VpL8FYJ
e4Q6XZgL46N6ObMe8diSvZI9f1S82XQAKR4YsiPF6ltRuGF4lzwZ/fGStW/Hcsaj
ubly9MQSC/bBoPTaaLCfo8S5
-----END PRIVATE KEY-----
";


    /**
     * 获取私钥
     * @return bool|resource
     */
    private static function getPrivateKey()

    {

        $privKey = self::$PRIVATE_KEY;

        return openssl_pkey_get_private($privKey);

    }


    /**
     * 获取公钥
     * @return bool|resource
     */
    private static function getPublicKey()

    {

        $publicKey = self::$PUBLIC_KEY;

        return openssl_pkey_get_public($publicKey);

    }



    /**
     * 私钥加密
     * @param string $data
     * @return null|string
     */
    public static function privateEncrypt($data = '')

    {

        if (!is_string($data)) {

            return null;

        }

        return openssl_private_encrypt($data, $encrypted, self::getPrivateKey()) ? base64_encode($encrypted) : null;

    }


    /**
     * 公钥加密
     * @param string $data
     * @return null|string
     */
    public static function publicEncrypt($data = '')

    {

        if (!is_string($data)) {

            return null;

        }

        return openssl_public_encrypt($data, $encrypted, self::getPublicKey()) ? base64_encode($encrypted) : null;

    }


    /**
     * 私钥解密
     * @param string $encrypted
     * @return null
     */
    public static function privateDecrypt($encrypted = '')

    {

        if (!is_string($encrypted)) {

            return null;

        }

        return (openssl_private_decrypt(base64_decode($encrypted), $decrypted, self::getPrivateKey())) ? $decrypted : null;

    }

    /**
     * 公钥解密
     * @param string $encrypted
     * @return null
     */
    public static function publicDecrypt($encrypted = '')

    {

        if (!is_string($encrypted)) {

            return null;

        }

        return (openssl_public_decrypt(base64_decode($encrypted), $decrypted, self::getPublicKey())) ? $decrypted : null;

    }
}